package com.train.chatspring.controller;

import com.train.chatspring.model.User;
import com.train.chatspring.service.FileService;
import com.train.chatspring.service.MailService;
import com.train.chatspring.service.UserService;
import jakarta.mail.internet.AddressException;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.MessagingException;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 聊天控制器，提供用户登录、注册、好友管理、文件上传下载等功能
 */
@RestController
@RequestMapping("/api/chat")
public class ChatController {

    @Autowired
    private UserService userService;

    @Autowired
    private FileService fileService;

    // 新增: 邮件服务注入
    @Autowired
    private MailService mailService;

    /**
     * 用户登录接口
     *
     * @param username 用户名
     * @return 登录结果
     */
    @PostMapping("/login")
    public ResponseEntity<String> login(@RequestParam String username) {
        if (userService.login(username)) {
            return ResponseEntity.ok("登录成功");
        }
        return ResponseEntity.badRequest().body("用户不存在");
    }

    /**
     * 用户注册接口
     *
     * @param user 用户对象
     * @return 注册结果
     */
    @PostMapping("/register")
    public ResponseEntity<String> register(@RequestBody User user) {
        userService.register(user);
        return ResponseEntity.ok("注册成功");
    }

    /**
     * 添加好友接口
     *
     * @param userId   用户ID
     * @param friendId 好友ID
     * @return 添加结果
     */
    @PostMapping("/add-friend")
    public ResponseEntity<String> addFriend(@RequestParam String userId, @RequestParam String friendId) {
        userService.addFriend(userId, friendId);
        return ResponseEntity.ok("好友添加成功");
    }

    /**
     * 获取好友列表接口
     *
     * @param userId 用户ID
     * @return 好友列表
     */
    @GetMapping("/friends")
    public ResponseEntity<List<String>> getFriends(@RequestParam String userId) {
        return ResponseEntity.ok(userService.getFriends(userId));
    }

    /**
     * 搜索好友接口
     *
     * @param userId   用户ID
     * @param friendId 好友ID
     * @return 是否找到好友
     */
    @GetMapping("/search-friend")
    public ResponseEntity<Boolean> searchFriend(@RequestParam String userId, @RequestParam String friendId) {
        return ResponseEntity.ok(userService.searchFriend(userId, friendId));
    }

    /**
     * 发送邮件接口
     *
     * @param to 收件人邮箱
     * @param subject 邮件主题
     * @param content 邮件正文
     * @return 发送结果
     */
    @PostMapping("/mail")
    public ResponseEntity<String> sendMail(
            @RequestParam String to,
            @RequestParam String subject,
            @RequestParam String content) {
        try {
            mailService.sendSimpleMail(to, subject, content);
            return ResponseEntity.ok("邮件发送成功");
        } catch (MessagingException e) {
            return ResponseEntity.badRequest().body("邮件发送失败: " + e.getMessage());
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("未知错误: " + e.getMessage());
        }
    }

    /**
     * 文件上传接口
     *
     * @param file 文件对象
     * @return 上传结果
     */
    @PostMapping("/upload")
    public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) {
        try {
            String filePath = fileService.uploadFile(file);
            return ResponseEntity.ok("文件上传成功: " + filePath);
        } catch (IOException e) {
            return ResponseEntity.badRequest().body("文件上传失败");
        }
    }

    /**
     * 文件下载接口
     *
     * @param fileName 文件名
     * @return 文件内容
     */
    @GetMapping("/download")
    public ResponseEntity<byte[]> downloadFile(@RequestParam String fileName) {
        try {
            byte[] fileContent = fileService.downloadFile(fileName);
            return ResponseEntity.ok().header("Content-Disposition", "attachment; filename=" + fileName).body(fileContent);
        } catch (IOException e) {
            return ResponseEntity.notFound().build();
        }
    }
}

